package com.platform.netty.tcp.task;

import com.platform.common.manager.AsyncManager;
import com.platform.common.utils.netty.NettyUtil;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.common.utils.spring.SpringUtils;
import com.platform.module.domain.CalorRecord;
import com.platform.module.domain.EqAlarm;
import com.platform.module.service.ICalorRecordService;
import com.platform.module.service.IEqAlarmService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.TimerTask;

@Data
@Slf4j
public class CalorPoolTask {

    private static ICalorRecordService calorRecordService;

    private static RedisUtil redisUtil;

    public static TimerTask saveCalor(final String datas, final Date createTime)
    {
        calorRecordService = SpringUtils.getBean(ICalorRecordService.class);

        redisUtil = SpringUtils.getBean(RedisUtil.class);

        return new TimerTask() {
            @Override
            public void run() {
                String[] contents = datas.split(" ");
                int datalength = contents.length;
                if(datalength==232){
                    saveCalor01(contents, createTime);
                }else if(datalength==185){
                    saveCalor02(contents, createTime);
                }else if(datalength==82){
                    saveCalor03(contents, createTime);
                }else if(datalength==63){
                    saveCalor04(contents, createTime);
                }
            }
        };
    }

    /**
     * 将兰吉尔获取的数据保存到数据库
     * @param contents
     */
    private static void saveCalor01(String[] contents, Date createTime){
        DecimalFormat format = new DecimalFormat("0.000");
        //热表id
        String calorId = contents[10]+contents[9]+contents[8]+contents[7];
        log.info("热表id" + calorId);
        try {
            //热量
            String rl = Integer.parseInt(contents[28] + contents[27]) + "." + Integer.parseInt(contents[30] + contents[29]);
            log.info("热量" + rl);
            //流量
            String ll = contents[36] + contents[35] + contents[34] + contents[33];
            //流量单位
            String lldw = contents[32];
            if (lldw.equals("14")) {
                ll = format.format(Float.parseFloat(ll) * 0.01);
            } else if (lldw.equals("15")) {
                ll = format.format(Float.parseFloat(ll) * 0.1);
            }
            log.info("流量" + ll);
            //功率
            String gl = "0";
            //功率单位
            String gldw = contents[38];
            if (contents[41].equals("F0")) {
                gl = contents[41] + contents[40] + contents[39];
                if (gldw.equals("2D")) {
                    gl = format.format(Float.parseFloat(gl) * 0.1);
                }
            }
            log.info("功率" + gl);
            //瞬时流量
            String ssll = "0";
            if (!contents[46].equals("F0")) {
                ssll = contents[46] + contents[45] + contents[44];
                ssll = format.format(Float.parseFloat(ssll) * 0.001);
            }
            log.info("瞬时流量" + ssll);
            //进水温度
            String jswd = format.format(Float.parseFloat(contents[50] + contents[49]) * 0.1);
            log.info("进水温度" + jswd);
            //回水温度
            String hswd = format.format(Float.parseFloat(contents[55] + contents[54]) * 0.1);
            log.info("回水温度" + hswd);
            //工作时间
            String gzsj = Integer.parseInt(contents[83] + contents[82] + contents[81] + contents[80]) + "";
            log.info("工作时间" + gzsj);
            //运行时间
            String yxsj = contents[223] + contents[222] + contents[221] + contents[220];
            //将16进制数据转为二进制数组
            String str = NettyUtil.hexStringBinStr(yxsj);
            String[] strs = str.split(" ");
            //计算分钟
            String mm = NettyUtil.get10Num(strs[26] + strs[27] + strs[28] + strs[29] + strs[30] + strs[31]).toString();
            if (mm.length() < 2) {
                mm = "0" + mm;
            }
            //计算小时
            String HH = NettyUtil.get10Num(strs[19] + strs[20] + strs[21] + strs[22] + strs[23]).toString();
            if (HH.length() < 2) {
                HH = "0" + HH;
            }
            //计算日期
            String MM = NettyUtil.get10Num(strs[4] + strs[5] + strs[6] + strs[7]).toString();
            if (MM.length() < 2) {
                MM = "0" + MM;
            }
            //计算月份
            String DD = NettyUtil.get10Num(strs[11] + strs[12] + strs[13] + strs[14] + strs[15]).toString();
            if (DD.length() < 2) {
                DD = "0" + DD;
            }
            //计算年份
            String YY = "20" + NettyUtil.get10Num(strs[17] + strs[18] + strs[0] + strs[1] + strs[2] + strs[3] + strs[8] + strs[9] + strs[10]).toString();
            log.info("表时间：" + YY + "-" + MM + "-" + DD + " " + HH + ":" + mm);

            yxsj = YY + "-" + MM + "-" + DD + " " + HH + ":" + mm;

            CalorRecord record = new CalorRecord();
            record.setCalorimeterId(calorId);
            record.setHeat(rl);
            record.setFlow(ll);
            record.setPower(gl);
            record.setInstant(ssll);
            record.setInlet(jswd);
            record.setOutlet(hswd);
            record.setWorkTime(gzsj);
            record.setGatherTime(yxsj);
            if(createTime==null) {
                record.setCreateTime(new Date());
            }else{
                record.setCreateTime(createTime);
            }
            calorRecordService.insertCalorRecord(record);
        }catch (Exception e){
            e.printStackTrace();

            AsyncManager.me().execute(AlarmPoolTask.saveAlarm(calorId, "2", "1", "采集兰吉尔热表数据", "兰吉尔热表"+calorId+"数据有误，解析错误"+e.getMessage()));
        }
    }

    /**
     * 将恩乐曼获取的数据保存到数据库
     * @param contents
     */
    private static void saveCalor02(String[] contents, Date createTime){
        DecimalFormat format = new DecimalFormat("0.000");
        //热表id
        String calorId = contents[10]+contents[9]+contents[8]+contents[7];
        log.info("热表id" + calorId);
        try {
            //热量
            String rl = Integer.parseInt(contents[28] + contents[27]) + "." + Integer.parseInt(contents[30] + contents[29]);
            log.info("热量" + rl);
            //流量
            String ll = contents[36] + contents[35] + contents[34] + contents[33];
            ll = format.format(Float.parseFloat(ll) * 0.01);
            log.info("流量" + ll);
            //功率
            String gl = contents[42] + contents[41] + contents[40] + contents[39];
            gl = format.format(Float.parseFloat(gl) * 0.001);
            log.info("当前功率" + gl);
            //瞬时流量
            String ssll = contents[54] + contents[53] + contents[52] + contents[51];
            ssll = format.format(Float.parseFloat(ssll) * 0.001);
            log.info("瞬时流量" + ssll);
            //进水温度
            String jswd = format.format(Float.parseFloat(NettyUtil.get10HexNum(contents[64]) + NettyUtil.get10HexNum(contents[63]).toString()) * 0.1);
            log.info("进水温度" + jswd);
            //回水温度
            String hswd = format.format(Float.parseFloat(NettyUtil.get10HexNum(contents[68]) + NettyUtil.get10HexNum(contents[67]).toString()) * 0.1);
            log.info("回水温度" + hswd);
            //工作时间
            String gzsj = Integer.parseInt(NettyUtil.get10HexNum(contents[76]) + NettyUtil.get10HexNum(contents[75]).toString()) * 60 + "";
            log.info("工作时间" + gzsj);
            //运行时间
            String yxsj = contents[82] + contents[81] + contents[80] + contents[79];
            //将16进制数据转为二进制数组
            String str = NettyUtil.hexStringBinStr(yxsj);
            String[] strs = str.split(" ");
            //计算分钟
            String mm = NettyUtil.get10Num(strs[26] + strs[27] + strs[28] + strs[29] + strs[30] + strs[31]).toString();
            if (mm.length() < 2) {
                mm = "0" + mm;
            }
            //计算小时
            String HH = NettyUtil.get10Num(strs[19] + strs[20] + strs[21] + strs[22] + strs[23]).toString();
            if (HH.length() < 2) {
                HH = "0" + HH;
            }
            //计算日期
            String MM = NettyUtil.get10Num(strs[4] + strs[5] + strs[6] + strs[7]).toString();
            if (MM.length() < 2) {
                MM = "0" + MM;
            }
            //计算月份
            String DD = NettyUtil.get10Num(strs[11] + strs[12] + strs[13] + strs[14] + strs[15]).toString();
            if (DD.length() < 2) {
                DD = "0" + DD;
            }

            yxsj = "2020-" + MM + "-" + DD + " " + HH + ":" + mm;

            CalorRecord record = new CalorRecord();
            record.setCalorimeterId(calorId);
            record.setHeat(rl);
            record.setFlow(ll);
            record.setPower(gl);
            record.setInstant(ssll);
            record.setInlet(jswd);
            record.setOutlet(hswd);
            record.setWorkTime(gzsj);
            record.setGatherTime(yxsj);
            if(createTime==null) {
                record.setCreateTime(new Date());
            }else{
                record.setCreateTime(createTime);
            }
            calorRecordService.insertCalorRecord(record);
        }catch (Exception e){
            e.printStackTrace();

            AsyncManager.me().execute(AlarmPoolTask.saveAlarm(calorId, "2", "1", "采集恩乐曼热表数据", "恩乐曼热表"+calorId+"数据有误，解析错误"+e.getMessage()));
        }
    }

    /**
     * 将代傲获取的数据保存到数据库
     * @param contents
     */
    private static void saveCalor03(String[] contents, Date createTime){
        DecimalFormat format = new DecimalFormat("0.000");
        //热表id
        String calorId = contents[10]+contents[9]+contents[8]+contents[7];
        log.info("热表id" + calorId);
        try {
            //热量
            String rl = Integer.parseInt(contents[22] + contents[21]) + "." + Integer.parseInt(contents[24] + contents[23]);
            ;
            log.info("热量" + rl);
            //流量
            String ll = contents[30] + contents[29] + contents[28] + contents[27];
            //热量单位
            String lldw = contents[26];
            if (lldw.equals("13")) {
                ll = format.format(Float.parseFloat(ll) * 0.001);
            } else if (lldw.equals("14")) {
                ll = format.format(Float.parseFloat(ll) * 0.01);
            } else if (lldw.equals("15")) {
                ll = format.format(Float.parseFloat(ll) * 0.1);
            }
            log.info("流量" + ll);
            String gldw = contents[32];
            //当前功率
            String gl = contents[36] + contents[35] + contents[34] + contents[33];
            if (contents[35].equals("DD") && contents[36].equals("DD")) {
                gl = "0";
            } else {
                gl = format.format(Float.parseFloat(gl) * 0.001);
            }
            log.info("当前功率" + gl);
            String ssll = contents[41] + contents[40] + contents[39];
            if (contents[41].equals("DD")) {
                ssll = "0";
            } else {
                ssll = format.format(Float.parseFloat(ssll) * 0.001);
            }
            log.info("瞬时流量" + ssll);
            //进水温度
            String jswd = format.format(Float.parseFloat(contents[45] + contents[44]) * 0.1);
            log.info("进水温度" + jswd);
            //回水温度
            String hswd = format.format(Float.parseFloat(contents[49] + contents[48]) * 0.1);
            log.info("回水温度" + hswd);
            //工作时间
            String gzsj = Integer.parseInt(contents[57] + contents[56]) * 60 + "";
            log.info("工作时间" + gzsj);
            //运行时间
            String yxsj = contents[63] + contents[62] + contents[61] + contents[60];
            //将16进制数据转为二进制数组
            String str = NettyUtil.hexStringBinStr(yxsj);
            String[] strs = str.split(" ");
            //计算分钟
            String mm = NettyUtil.get10Num(strs[26] + strs[27] + strs[28] + strs[29] + strs[30] + strs[31]).toString();
            if (mm.length() < 2) {
                mm = "0" + mm;
            }
            //计算小时
            String HH = NettyUtil.get10Num(strs[19] + strs[20] + strs[21] + strs[22] + strs[23]).toString();
            if (HH.length() < 2) {
                HH = "0" + HH;
            }
            //计算日期
            String MM = NettyUtil.get10Num(strs[4] + strs[5] + strs[6] + strs[7]).toString();
            if (MM.length() < 2) {
                MM = "0" + MM;
            }
            //计算月份
            String DD = NettyUtil.get10Num(strs[11] + strs[12] + strs[13] + strs[14] + strs[15]).toString();
            if (DD.length() < 2) {
                DD = "0" + DD;
            }
            //计算年份
            String YY = "20" + NettyUtil.get10Num(strs[17] + strs[18] + strs[0] + strs[1] + strs[2] + strs[3] + strs[8] + strs[9] + strs[10]).toString();
            log.info("表时间：" + YY + "-" + MM + "-" + DD + " " + HH + ":" + mm);

            yxsj = YY + "-" + MM + "-" + DD + " " + HH + ":" + mm;

            CalorRecord record = new CalorRecord();
            record.setCalorimeterId(calorId);
            record.setHeat(rl);
            record.setFlow(ll);
            record.setPower(gl);
            record.setInstant(ssll);
            record.setInlet(jswd);
            record.setOutlet(hswd);
            record.setWorkTime(gzsj);
            record.setGatherTime(yxsj);
            if(createTime==null) {
                record.setCreateTime(new Date());
            }else{
                record.setCreateTime(createTime);
            }
            calorRecordService.insertCalorRecord(record);
        }catch (Exception e){
            e.printStackTrace();

            AsyncManager.me().execute(AlarmPoolTask.saveAlarm(calorId, "2", "1", "采集代傲热表数据", "代傲热表"+calorId+"数据有误，解析错误"+e.getMessage()));
        }
    }

    /**
     * 将金水获取的数据保存到数据库
     * @param contents
     */
    private static void saveCalor04(String[] contents, Date createTime){
        DecimalFormat format = new DecimalFormat("0.000");
        //热表id
        String calorId = contents[9] + contents[8] + contents[7] + contents[6];
        log.info("热表id" + calorId);
        try {
            //热量
            String rl = contents[26] + contents[25] + contents[24] + contents[23];
            //热量单位
            String rldw = contents[27];
            if (rldw.equals("05")) {
                rl = format.format(Float.parseFloat(rl) * 0.01);
            } else if (rldw.equals("06")) {
                rl = format.format(Float.parseFloat(rl));
            }
            log.info("热量" + rl);
            //流量
            String ll = format.format(Float.parseFloat(contents[41] + contents[40] + contents[39] + "." + contents[38]));
            log.info("流量" + ll);
            //当前功率
            String gl = format.format(Float.parseFloat(contents[31] + contents[30] + contents[29] + "." + contents[28]));
            log.info("当前功率" + gl);
            String ssll = format.format(Float.parseFloat(contents[36] + contents[35] + "." + contents[34] + contents[33]));
            log.info("瞬时流量" + ssll);
            //进水温度
            String jswd = format.format(Float.parseFloat(contents[44] + "." + contents[43]));
            log.info("进水温度" + jswd);
            //回水温度
            String hswd = format.format(Float.parseFloat(contents[47] + "." + contents[46]));
            log.info("回水温度" + hswd);
            //工作时间
            String gzsj = contents[51] + contents[50] + contents[49] + "";
            log.info("工作时间" + gzsj);
            //运行时间
            String yxsj = "20" + contents[57] + "-" + contents[56] + "-" + contents[55] + " " + contents[54] + ":" + contents[53] + ":" + contents[52];
            log.info("表时间：" + yxsj);

            CalorRecord record = new CalorRecord();
            record.setCalorimeterId(calorId);
            record.setHeat(rl);
            record.setFlow(ll);
            record.setPower(gl);
            record.setInstant(ssll);
            record.setInlet(jswd);
            record.setOutlet(hswd);
            record.setWorkTime(gzsj);
            record.setGatherTime(yxsj);
            if(createTime==null) {
                record.setCreateTime(new Date());
            }else{
                record.setCreateTime(createTime);
            }
            calorRecordService.insertCalorRecord(record);
        }catch (Exception e){
            e.printStackTrace();

            AsyncManager.me().execute(AlarmPoolTask.saveAlarm(calorId, "2", "1", "采集金水热表数据", "金水热表"+calorId+"数据有误，解析错误"+e.getMessage()));
        }
    }
}
